"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 资源池化
Case Name   : 执行计划(explain):索引
Create At   : 2023.5
Owner       : opentestcase009
Description :
    1、查看集群状态
    2、创建表,插入数据
    3、创建btree索引
    4、关闭seqscan，explain结合select语句使用，打印执行信息
        set enable_seqscan to off;
    5、打开seqscan，explain结合select语句使用，打印执行信息
        set enable_seqscan to on;
    6、清理环境
Expect      :
    1、集群状态正常,数据库节点状态正常
    2、建表成功,插入数据成功
    3、创建索引成功
    4、成功
    5、成功
    6、清理环境成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class SharedStorage(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.comsh = CommonSH('PrimaryDbUser')
        self.t_name = 't_dms_page_018'
        self.i_name = 'i_dms_page_018'

    def test_shared_storage(self):
        text = '-----step1:查看集群状态; expect:集群状态正常-----'
        self.log.info(text)

        status = self.comsh.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)

        text = '-----step2:创建表,插入数据; expect:建表成功,插入数据成功-----'
        self.log.info(text)

        sql = f'''create table {self.t_name} (stu_id varchar(5) not null,
            score int,num int) with(segment=on);
            insert into {self.t_name} values('01', 90, 
            generate_series(1,500));'''
        self.log.info(sql)
        res = self.comsh.execut_db_sql(sql)
        self.log.info(res)
        self.assertTrue(self.constant.CREATE_TABLE_SUCCESS in res
                        and self.constant.INSERT_SUCCESS_MSG in res,
                        '执行失败' + text)

        text = '-----step3:创建btree索引; expect:成功-----'
        self.log.info(text)

        index = f'''create index {self.i_name} 
            on {self.t_name} using btree(num);'''
        self.log.info(index)

        cre_index = self.comsh.execut_db_sql(index)
        self.log.info(cre_index)
        self.assertIn(self.constant.CREATE_INDEX_SUCCESS, cre_index,
                      '执行失败' + text)

        text = '----step4:关闭seqscan,explain结合select语句使用,打印执行信息; expect:成功----'
        self.log.info(text)

        explain1 = f'''set enable_seqscan to off;
            explain select * from {self.t_name} where num > 100;'''
        self.log.info(explain1)

        exec_exp1 = self.comsh.execut_db_sql(explain1)
        self.log.info(exec_exp1)
        self.assertIn('Bitmap Heap Scan', exec_exp1, '执行结果异常')

        text = '----step5:打开seqscan,explain结合select语句使用,打印执行信息; expect:成功----'
        self.log.info(text)

        explain2 = f'''set enable_seqscan to on;
            explain select * from {self.t_name} where num > 100;'''
        self.log.info(explain2)

        exec_exp2 = self.comsh.execut_db_sql(explain2)
        self.log.info(exec_exp2)
        self.assertIn('Seq Scan', exec_exp2, '执行结果异常')

    def tearDown(self):
        text = '-----step6:清理环境,删除表; expect:清理环境成功-----'
        self.log.info(text)

        clear = f'''drop table {self.t_name} cascade;'''
        self.log.info(clear)
        clear_res = self.comsh.execut_db_sql(clear)
        self.log.info(clear_res)

        self.assertIn(self.constant.DROP_TABLE_SUCCESS, clear_res,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
